<?php
/**
 * News index controller
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package News Module
 * @subpackage Controller
 * @license  New BSD License
 *
 */
class News_IndexController extends Bel_Controller{

	/**
	 * News model
	 *
	 * @var Models_News
	 */
	private $_news;
	
	/**
	 * Initialization
	 *
	 */
	public function init() {
		$this->_news = new Models_News ( );
		parent::init ();
	}
	
	/**
	 * Displays list of news with paging. Each page has 5 news entries 
	 *
	 */
	public function indexAction() {
		$this->_news->setItemsOnPage(5);		
		$this->view->assign ( 'news_list', $this->_news->getAllNews($this->_request->getParam('page')) );
		$this->view->assign('paging',$this->_news->getPaging('/news/?page='));
		$this->view->display ( 'news/list.tpl' );
	}
	
	/**
	 * Displays full news entry
	 *
	 */
	public function itemAction() {		
		if (($newsItem = $this->_news->getEntryById( $this->_request->getParam ( 'news_id' ) ))) {
			$this->_seo->setTitle ( $newsItem->news_title );
			$this->_seo->setDescription(substr(strip_tags($newsItem->news_content),0,50).'...');			
			$this->view->assign ( 'newsItem', $newsItem );
			$this->view->display ( 'news/item.tpl' );
		} else {
			$this->_response->setHeader('HTTP/1.x', '404 Not found');
			$this->_response->clearBody();
        	$this->_response->setBody($this->view->fetch('404.html'));
        	$this->_response->sendHeaders();
		}
	}
}